package cn.jr.dao.impl;

import cn.jr.dao.IUserDao;
import cn.jr.pojo.ShopRun;
import cn.jr.util.DBHelper;
import org.w3c.dom.ls.LSOutput;

import java.util.ArrayList;

public class UserDaoImpl implements IUserDao {
    DBHelper db=new DBHelper();
    @Override
    public int insertUser(ShopRun shop) {
        /**
         * 添加用户
         * @param shop 用户对象
         * @return 影响行数
         */
        String sql="insert into tshoprun values(?,?,?)";
        if(selectUserByUserName(shop.getUserName())==null){
            int UserId1 =Integer.parseInt(selectUserEnd().getUserId().substring(4))+1;
            shop.setUserId("user"+UserId1);
            Object[] objs={shop.getUserId(),shop.getUserName(),shop.getPassword()};
            return db.dtUpdate(sql,objs);
        }else {
            return 0;
        }

    }

    @Override
    public ShopRun selectUserByUserName(String userName) {
        /**根据用户名查询用户
         * @param userName 用户名
         * @return 用户信息
         */
        String sql="select * from tshoprun where userName=?";
        ArrayList list = db.dtQuery(sql,ShopRun.class,userName);

        return list.size()>0?(ShopRun)list.get(0):null;
    }

    @Override
    public ShopRun selectUserEnd() {
        /**
         * 查询最后一条数据的用户编号
         * @return 用户编号
         */
        String sql ="select * from tshoprun";
        ArrayList list = db.dtQuery(sql,ShopRun.class);
        return (ShopRun)list.get(list.size()-1);
    }

    @Override
    public String selectPassByUserId(String userId) {
        /**根据用户表主键查询密码
         * @param userId 用户表主键
         * @return 用户密码
         */
        String sql="select * from tshoprun where userId=?";
        ArrayList<ShopRun> list = db.dtQuery(sql,ShopRun.class,userId);
        return list.size()>0?list.get(0).getPassword():null;
    }
}
